package cn.edu.xmu.tangzk.scala.s99

import scala.collection.immutable.Nil

object P08 {

    def compressRecursive(ls: List[Any]): List[Any] = {
        ls match {
            case Nil => Nil
            case h :: tail => h :: compressRecursive(tail.dropWhile(_ == h))
        }
    }

    def compressFromRight[A](ls: List[A]): List[A] = {
        ls.foldRight(List[A]()) { (h, r) =>
            if (r.isEmpty || r.head != h) h :: r
            else r
        }
    }

    def compressFromLeft[A](ls: List[A]): List[A] = {
        ls.foldLeft(List[A]()) { (h, r) =>
            if (h.isEmpty || h.last != r) h ::: List(r)
            else h
        }
    }

    def main(args: Array[String]) {
        println(compressFromLeft(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))
    }

}